package com.yonyou.crm.csns.im.appctrl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.http.client.CrmHttpClient;
import com.yonyou.crm.common.http.client.CrmResponse;
import com.yonyou.crm.common.rest.constant.AppCodeConstant;
import com.yonyou.crm.common.rest.constant.HttpCodeConstant;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.csns.im.constant.ImConstant;
import com.yonyou.crm.csns.im.rmitf.IImRmService;
import com.yonyou.iuap.context.InvocationInfoProxy;

@RestController
@RequestMapping(value="/{version}/mcsns/im")
public class AppImCtrl {
	
	private final static String ImBaseDir = "https://im.yyuap.com/";
	
	@Autowired
	private IImRmService service;
	
	/**
	 * 获取 euc im 的usertoken
	 * @param version
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "utoken", method = RequestMethod.POST)
	public Object getUserToken(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		String userId = InvocationInfoProxy.getParameter("id");
		
		if (StringUtils.isBlank(userId)) {
			throw new CrmMessageException(AppCodeConstant.ServerError, "获取当前登录用户信息失败");
		}
		
		Map<String, Object> imMap = service.getImInfo(new Long(userId));
		
		return ResponseUtil.getObject(getUserToken(imMap));
	}
	
	/**
	 * 向IM发送获取用户token请求
	 * @param imMap
	 * @return
	 * @throws CrmMessageException
	 */
	private CrmResponse sendUserTokenRequest(Map<String, Object> imMap) throws CrmMessageException {
		

		
		if (imMap == null || imMap.size() == 0) {
			throw new CrmMessageException(AppCodeConstant.ServerError, "获取IM认证信息失败");
		}

		String eptId = (String) imMap.get(ImConstant.EptId);
		String appId = (String) imMap.get(ImConstant.AppId);
		String url = ImBaseDir + "sysadmin/rest/"+ eptId +"/"+ appId +"/token";
				
		JSONObject paramObj = new JSONObject();
		paramObj.put(ImConstant.ClientId, imMap.get(ImConstant.ClientId));
		paramObj.put(ImConstant.ClientSecret, imMap.get(ImConstant.ClientSecret));
		paramObj.put("userid", imMap.get("id"));
		paramObj.put("nickname", imMap.get("name"));
		/*头像信息暂时为固定值，确定文件上传形式后，此处从数据库中获取*/
		paramObj.put("photo", getPhoto(imMap.get("id")));

		return CrmHttpClient.post(url, paramObj);
	}
	
	private String getPhoto(Object idObj) {
		
		if (idObj == null) {
			return "";
		}
		
		String id = idObj.toString();

		if (id != null && id.length() > 0) {
			Map<String, String> photoMap = new HashMap<String, String>();
			photoMap.put("2", "http://img4.imgtn.bdimg.com/it/u=3587788205,2073078615&fm=214&gp=0.jpg");
			photoMap.put("60", "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1191873587,3864918266&fm=27&gp=0.jpg ");
			photoMap.put("61", "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3242709860,2221903223&fm=27&gp=0.jpg");
			photoMap.put("62", "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1191873587,3864918266&fm=27&gp=0.jpg");
			photoMap.put("63", "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1435749699,2501486107&fm=27&gp=0.jpg");
			photoMap.put("65", "https://ss0.baidu.com/73t1bjeh1BF3odCf/it/u=377818163,1111141501&fm=85&s=ECCA7A2B4CB225B35498150E010080A1");
			photoMap.put("66", "https://ss0.baidu.com/73x1bjeh1BF3odCf/it/u=2033816434,820626626&fm=85&s=D23E3CC4D6D9912E31101C790300C050");
			photoMap.put("67", "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1191873587,3864918266&fm=27&gp=0.jpg");
			photoMap.put("68", "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3242709860,2221903223&fm=27&gp=0.jpg");
			photoMap.put("70", "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1191873587,3864918266&fm=27&gp=0.jpg");
			photoMap.put("71", "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1435749699,2501486107&fm=27&gp=0.jpg");

			String photo = photoMap.get(id);
			return photo==null ? "http://img4.imgtn.bdimg.com/it/u=3587788205,2073078615&fm=214&gp=0.jpg" : photo;
		}
		
		return "";
	}
	
	/**
	 * 获得用户token字符串
	 * @param imMap
	 * @return
	 * @throws CrmMessageException
	 */
	private JSONObject getUserToken(Map<String, Object> imMap) throws CrmMessageException {

		CrmResponse cResp = sendUserTokenRequest(imMap);
		JSONObject atObj = JSONObject.fromObject(cResp.getData());

		if (cResp.getCode() != HttpCodeConstant.OK) {
			
			if (atObj != null && atObj.containsKey("message")) {
				
				String message = (String) atObj.get("message");
				throw new CrmMessageException(AppCodeConstant.ServerError ,message);
			}
		}

		return atObj;
	}

}

